#ifndef __CSP_UART_H__
#define __CSP_UART_H__

//#include "AC33Mx128.h"
typedef struct {

	CSP_REGISTER_T		UnRBR_UnTHR; 		// offset = 0x0000, RO/WO
	CSP_REGISTER_T		UnIER;				// offset = 0x0004, R/W
	CSP_REGISTER_T		UnIIR; 				// offset = 0x0008, RO
	CSP_REGISTER_T		UnLCR; 				// offset = 0x000C, R/W

	CSP_REGISTER_T		UnDCR;				// offset = 0x0010, R/W
	CSP_REGISTER_T		UnLSR;				// offset = 0x0014, RO
	CSP_REGISTER_T		UnMSR;				// offset = 0x0018, RO 
	CSP_REGISTER_T		UnSCR;				// offset = 0x001C, R/W 

	CSP_REGISTER_T		UnBDR;				// offset = 0x0020, R/W
	CSP_REGISTER_T		UnBFR;				// offset = 0x0024, R/W 
	CSP_REGISTER_T		res28;				// offset = 0x0028
	CSP_REGISTER_T		res2C;				// offset = 0x002C

	CSP_REGISTER_T		UnIDTR;				// offset = 0x0030, R/W 


} CSP_UART_T; 


//=======================================================================
// D A T A    S T R U C T U R E
//=======================================================================
typedef struct {
	UINT32 		u32Baudrate;
	UINT16		u16DataBits;
	UINT8		u8StopBits;
	UINT8		u8Parity;
	UINT32		u32Reference_clock; 
} UART_PROPERTY; 



//=======================================================================
// Definitions
//=======================================================================
#define N_UART						(4)


//==========================================================================
// 	UnRBR/UnTHR 
//		
//
//==========================================================================


//==========================================================================
// 	UnIER
//		
//				U0IER			@ address = 0x4000_8004
//				U1IER			@ address = 0x4000_8104
//				U2IER			@ address = 0x4000_8204
//
//
//==========================================================================
#define UnIER_DTXIEN					(0x0001<<5)
#define UnIER_DRXIEN					(0x0001<<4)
#define UnIER_RLSIE						(0x0001<<2)
#define UnIER_THREIE					(0x0001<<1)
#define UnIER_DRIE						(0x0001<<0)

#define UnIER_INTR_MASK					(0x0037<<0)



//==========================================================================
// 	UnIIR 
//	
//				U0IIR			@ address = 0x4000_8008
//				U1IIR			@ address = 0x4000_8108
//				U2IIR			@ address = 0x4000_8208
//
//==========================================================================
#define UnIIR_IID_DMA					(0x0001<<3)

#define UnIIR_IID_TXBUF_EMPTY			(0x0001<<1)
#define UnIIR_IID_RXBUF_READY			(0x0002<<1)
#define UnIIR_IID_LINE_STATUS			(0x0003<<1)

#define UnIIR_IPEN_NO_PENDING			(0x0001<<0)
#define UnIIR_IPEN_PENDING				(0x0000<<0)

#define UnIIR_INTR_MASK 				(0x00C7<<0)
#define UnIIR_INTR_MASK_NO_DMA			(0x0007<<0)


#define UnIIR_IID_DMA_RX_READY			(UnIIR_IID_DMA|UnIIR_IID_RXBUF_READY)
#define UnIIR_IID_DMA_TX_EMPTY			(UnIIR_IID_DMA|UnIIR_IID_TXBUF_EMPTY)


//==========================================================================
// 	UnLCR
//	
//				U0LCR			@ address = 0x4000_800C
//				U1LCR			@ address = 0x4000_810C
//				U2LCR			@ address = 0x4000_820C
//
//
//==========================================================================
#define UnLCR_BREAK							(0x0001<<6)
#define UnLCR_STICKP						(0x0001<<5)
#define UnLCR_PARITY						(0x0001<<4)
#define UnLCR_PEN							(0x0001<<3)

#define UnLCR_STOPBIT						(0x0001<<2)
#define UnLCR_STOPBIT_1_STOP_BIT			(0x0000<<2)
#define UnLCR_STOPBIT_2_STOP_BITS			(0x0001<<2)

#define UnLCR_DLEN_5_BITS					(0x0000<<0)
#define UnLCR_DLEN_6_BITS					(0x0001<<0)
#define UnLCR_DLEN_7_BITS					(0x0002<<0)
#define UnLCR_DLEN_8_BITS					(0x0003<<0)
#define UnLCR_DLEN_MASK						(0x0003<<0)





//==========================================================================
// 	UnLSR
//	
//				U0LSR			@ address = 0x4000_8014
//				U1LSR			@ address = 0x4000_8114
//				U2LSR			@ address = 0x4000_8214
//
//
//==========================================================================
#define UnLSR_TEMT							(0x0001<<6)
#define UnLSR_THRE							(0x0001<<5)
#define UnLSR_BI							(0x0001<<4)
#define UnLSR_FE							(0x0001<<3)
#define UnLSR_PE							(0x0001<<2)
#define UnLSR_OE							(0x0001<<1)
#define UnLSR_DR							(0x0001<<0)




//=======================================================================
// MSR
//
//		Modem Status Register 
//
//			UART0: 0x4000_B018
//			UART1: 0x4000_C018
//			UART2: 0x4000_D018
//
//=======================================================================
//	
//		DCD			Data Carrier Detect
//					0 = nDCD High
//					1 = nDCD Low
//
//		RI			Complement of nRI 
//					0 = nRI High
//					1 = nRI Low
//
//		DSR			Data Set Ready
//					0 = nDSR High
//					1 = nDSR Low
//
//		CTS			Clear To Send
//					0 = nCTS High
//					1 = nCTS Low
//
//		DDCD		Delta of DCD
//					0 = no change 
//					1 = the state of modem input DCD changes
//
//		TERI			Trailing Edge Ring Indicator
//					0 
//					1 = a low-to-high transition occurs on the RI input 
//
//		DDSR		Delta DSR 
//					0 = no change 
//					1 = the state of modem input DSR changes
//
//		DCTS		Delta CTS
//					0 = no change 
//					1 = the stateof modem input CTS changes
//
//=======================================================================
#define MSR_DCD								(0x0001<<7)
#define MSR_RI								(0x0001<<6)
#define MSR_DSR								(0x0001<<5)
#define MSR_CTS								(0x0001<<4)
#define MSR_DDCD							(0x0001<<3)
#define MSR_TERI							(0x0001<<2)
#define MSR_DDSR							(0x0001<<1)
#define MSR_DCTS							(0x0001<<0)




//=======================================================================
// SCR
//
//		Scratch Register 
//
//			UART0: 0x4000_B01C
//			UART1: 0x4000_C01C
//			UART2: 0x4000_D01C
//
//=======================================================================



//==========================================================================
// 
//		M A C R O S
//
//==========================================================================
#define CSP_UART_GET_UnRBR(uart)				((uart)->UnRBR_UnTHR)
#define CSP_UART_SET_UnTHR(uart, val)			((uart)->UnRBR_UnTHR = (val))
//-----------------------------------------------------------------------------------------
#define CSP_UART_GET_UnIER(uart)				((uart)->UnIER)
#define CSP_UART_SET_UnIER(uart, val)			((uart)->UnIER = (val))
//-----------------------------------------------------------------------------------------
#define CSP_UART_GET_UnIIR(uart)				((uart)->UnIIR)
//-----------------------------------------------------------------------------------------
#define CSP_UART_GET_UnLCR(uart)				((uart)->UnLCR)
#define CSP_UART_SET_UnLCR(uart, val)			((uart)->UnLCR = (val))
//-----------------------------------------------------------------------------------------
#define CSP_UART_GET_UnDCR(uart)				((uart)->UnDCR)
#define CSP_UART_SET_UnDCR(uart, val)			((uart)->UnDCR = (val))
//-----------------------------------------------------------------------------------------
#define CSP_UART_GET_UnLSR(uart)				((uart)->UnLSR)
//-----------------------------------------------------------------------------------------
#define CSP_UART_GET_UnMSR(uart)				((uart)->UnMSR)
//-----------------------------------------------------------------------------------------
#define CSP_UART_GET_UnSCR(uart)				((uart)->UnSCR)
#define CSP_UART_SET_UnSCR(uart, val)			((uart)->UnSCR = (val))
//-----------------------------------------------------------------------------------------
#define CSP_UART_GET_UnBDR(uart)				((uart)->UnBDR)
#define CSP_UART_SET_UnBDR(uart, val)			((uart)->UnBDR = (val))
//-----------------------------------------------------------------------------------------
#define CSP_UART_GET_UnBFR(uart)				((uart)->UnBFR)
#define CSP_UART_SET_UnBFR(uart, val)			((uart)->UnBFR = (val))
//-----------------------------------------------------------------------------------------
#define CSP_UART_GET_UnIDTR(uart)				((uart)->UnIDTR)
#define CSP_UART_SET_UnIDTR(uart, val)			((uart)->UnIDTR = (val))
//-----------------------------------------------------------------------------------------




// UART
extern CSP_UART_T			* const			UART0;
extern CSP_UART_T			* const			UART1;
extern CSP_UART_T			* const			UART2;
extern CSP_UART_T			* const			UART3;



//==========================================================================
// 
//		F U N C T I O N    D E C L A R A T I O N S 
//
//==========================================================================
void CSP_UART_ConfigureGPIO (CSP_UART_T * const uart); 
void CSP_UART_Init (CSP_UART_T * const uart, UART_PROPERTY *property); 
void CSP_UART_SetBaudrate (CSP_UART_T * const uart, UINT32 baudrate, UINT32 prescaler, UINT32 ref_clk); 
void CSP_UART_Close (CSP_UART_T * const uart); 
void CSP_UART_ConfigureInterrupt (CSP_UART_T * const uart, UINT32 intr_mask, UINT32 enable); 


#endif 

